*******************************************************************************
** declare it is a panel data by using `xtset' 
egen Countrynum = group(countryisocode)
list country Countrynum in 1/10, sepby(Country)
xtset Countrynum year

drop if year < 2013

gen pr = 0
replace pr = 1 if electoral_rule == 2
gen mixed = 0
replace mixed = 1 if electoral_rule == 3

******************* Main Analysis using Modified Rainbow Index ***************
xtreg rainbow2 l.perception l.google l.lgbt_asso l.pr l.mixed l.gov_left2 l.eu l.Protestant l.Catholic l.Other l.openc l.realgdpgr i.year, cluster(Countrynum)
* Figure 3
margins, at(l.perception = (4 (1) 10) ) vsquish
marginsplot
* Figure 4
margins, at(l.google= (-12 (4) 12) ) vsquish
marginsplot

xtreg rainbow2 l.c.perception##l.c.google l.lgbt_asso l.pr l.mixed l.gov_left2 l.eu l.Protestant l.Catholic l.Other l.openc l.realgdpgr i.year, cluster(Countrynum)
* Figure 5, Right Panel
margins, at(l.google= (-12 (4) 12) l.perception = (4 9.78) ) vsquish
marginsplot, legend(position(6) col(2))

xtreg rainbow2 l.c.perception##l.c.pr l.google  l.lgbt_asso l.gov_left1 l.eu l.Protestant l.Catholic l.Other l.openc l.realgdpgr  i.year, cluster(Countrynum)
* Figure 6, Left Panel
margins, at(l.perception = (4 (1) 10) l.pr == (1 0)) vsquish
marginsplot, legend(position(6) col(2))

xtreg rainbow2 l.c.perception##l.c.mixed l.google  l.lgbt_asso l.gov_left1 l.eu l.Protestant l.Catholic l.Other l.openc l.realgdpgr  i.year, cluster(Countrynum)
* Figure 6, Right Panel
margins, at(l.perception = (4 (2) 10)  l.mixed == (1 0)) vsquish
marginsplot, legend(position(6) col(2))

xtreg rainbow2 l.c.perception##l.c.avemag l.google l.lgbt_asso l.gov_left1 l.eu l.Protestant l.Catholic l.Other l.openc l.realgdpgr  i.year, cluster(Countrynum)
* Figure 7
margins, at(l.perception = (4 (2) 10) l.avemag == (5.14 9 13.64)) vsquish
marginsplot, legend(position(6) col(3))

xtreg rainbow2 l.c.perception##l.c.electhre  l.google  l.lgbt_asso l.gov_left1 l.eu l.Protestant l.Catholic l.Other l.openc l.realgdpg i.year, cluster(Countrynum)
* Figure 8
margins, at(l.perception = (4 (2) 10) l.electhre == (0 3 5)) vsquish
marginsplot, legend(position(6) col(3))

xtreg rainbow2 l.c.perception##l.c.ccsystem l.google  l.lgbt_asso l.gov_left1 l.eu l.Protestant l.Catholic l.Other l.openc l.realgdpgr   i.year, cluster(Countrynum)
* Figure 9
margins, at(l.perception = (4 (2) 10) l.ccsystem == (0 1)) vsquish
marginsplot, legend(position(6) col(3))

******************* Main Analysis using Origianl Rainbow Index ***************
xtreg rainbow l.perception l.google l.lgbt_asso l.pr l.mixed l.gov_left2 l.eu l.Protestant l.Catholic l.Other l.openc l.realgdpgr i.year, cluster(Countrynum)
margins, at(l.perception = (4 (1) 10) ) vsquish
marginsplot
margins, at(l.google= (-12 (4) 12) ) vsquish
marginsplot

xtreg rainbow l.c.perception##l.c.google l.lgbt_asso  l.pr l.mixed l.gov_left2 l.eu l.Protestant l.Catholic l.Other l.openc  l.realgdpgr i.year, cluster(Countrynum)
* Figure 5, Left Panel
margins, at(l.google= (-12 (4) 12) l.perception = (4 9.78) ) vsquish
marginsplot, legend(position(6) col(2))

xtreg rainbow l.c.perception##l.c.pr l.google  l.lgbt_asso l.gov_left1 l.eu l.Protestant l.Catholic l.Other l.openc l.realgdpgr  i.year, cluster(Countrynum)
margins, at(l.perception = (4 (1) 10) l.pr == (1 0)) vsquish
marginsplot, legend(position(6) col(2))

xtreg rainbow l.c.perception##l.c.mixed l.google  l.lgbt_asso l.gov_left1 l.eu l.Protestant l.Catholic l.Other l.openc l.realgdpgr  i.year, cluster(Countrynum)
margins, at(l.perception = (4 (2) 10)  l.mixed == (1 0)) vsquish
marginsplot, legend(position(6) col(2))

xtreg rainbow l.c.perception##l.c.avemag l.google l.lgbt_asso l.gov_left1 l.eu l.Protestant l.Catholic l.Other l.openc l.realgdpgr  i.year, cluster(Countrynum)
margins, at(l.perception = (4 (2) 10) l.avemag == (4.26 8.42 13.64)) vsquish
marginsplot, legend(position(6) col(3))

xtreg rainbow l.c.perception##l.c.electhre  l.google  l.lgbt_asso l.gov_left1 l.eu l.Protestant l.Catholic l.Other l.openc l.realgdpg i.year, cluster(Countrynum)
margins, at(l.perception = (4 (2) 10) l.electhre == (0 (3) 7)) vsquish
marginsplot, legend(position(6) col(3))

xtreg rainbow l.c.perception##l.c.ccsystem l.google  l.lgbt_asso l.gov_left1 l.eu l.Protestant l.Catholic l.Other l.openc l.realgdpgr   i.year, cluster(Countrynum)
margins, at(l.perception = (4 (2) 10) l.ccsystem == (0 1)) vsquish
marginsplot, legend(position(6) col(3))

************** Appendix: interaction with government partisanship *************
xtreg rainbow l.c.perception##l.c.gov_left1 l.google  l.lgbt_asso l.gov_left1 l.eu l.Protestant l.Catholic l.Other l.openc l.realgdpgr  i.year, cluster(Countrynum)

xtreg rainbow2 l.c.perception##l.c.gov_left1 l.google  l.lgbt_asso l.gov_left1 l.eu l.Protestant l.Catholic l.Other l.openc l.realgdpgr  i.year, cluster(Countrynum)

margins, at(l.perception = (4 (2) 10)  l.c.gov_left1 == (0 (33) 99)) vsquish
marginsplot, legend(position(6) col(2))

